import java.util.Arrays;

/**
 * @Author 12629
 * @Date 2022/5/6 18:39
 * @Description：
 */
public class Test {

    public static void main(String[] args) {
        int[][] array = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[2][3];
        int[][] array3 = new int[][] {{1,2,3},{4,5,6}};

        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }

    }

    public static double avg(int[] array) {
        int sum = 0;
        for (int x : array) {
            sum += x;
        }
        return sum*1.0/array.length;
    }

    /**
     * 寻找关键字key 找到返回下标 找不到返回-1
     * @param key
     * @param array
     * @return
     */
    public static int findKey(int key,int[] array) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 二分查找：
     * 1、前提: 必须是有序的
     * @param array
     * @param key
     * @return
     */
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int mid = (left+right)/2;
            if(array[mid] == key) {
                return mid;
            }else if(array[mid] < key) {
                left = mid+1;
            }else{
                right = mid-1;
            }
        }
        return -1;
    }


    public static void bubbleSort(int[] array) {
        //boolean flg = false;
        //i代表的是趟数
        for (int i = 0; i < array.length-1; i++) {
            //j每一趟都从0开始
            boolean flg = false;
            for(int j = 0;j < array.length-1-i;j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(flg == false) {
                return;
            }
        }
    }

    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while (left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main13(String[] args) {
      /*  int[] array = {1,2,3,13,4,5,6};
        reverse(array);
        System.out.println(Arrays.toString(array));
        */
        int[] array2 = new int[10];
        Arrays.fill(array2,1,5,99);//[1,5)
        System.out.println(Arrays.toString(array2));
    }

    public static void main12(String[] args) {
        int[] array = {1,2,3,13,4,5,6};

        bubbleSort(array);
        //Arrays.sort(array);

        System.out.println("排序："+Arrays.toString(array));

        /*int index = Arrays.binarySearch(array,13);
        System.out.println(index);*/
        //System.out.println(avg(array));
    }


    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = array;
        System.out.println(Arrays.toString(array));
        System.out.println("=====================");
        System.out.println(Arrays.toString(copy));
    }

    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = array.clone();//clone():产生对象的一个副本
        System.out.println(Arrays.toString(array));
        System.out.println("=====================");
        System.out.println(Arrays.toString(copy));
    }

    public static void main_arraycopy(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];

        /**
         * 1、是你要拷贝的数组
         * 2、你要从这个数组的哪个下标开始拷贝
         * 3、你要拷贝到哪个数组
         * 4、你要拷贝到这个数组的哪个位置开始
         * 5、你要拷贝多大
         */
        System.arraycopy(array,0,copy,0,array.length);

        System.out.println(Arrays.toString(array));
        System.out.println("=====================");
        System.out.println(Arrays.toString(copy));
    }

    public static void mainCopyOf(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] copy = Arrays.copyOf(array,array.length*2);
        //int[] copy = Arrays.copyOfRange(array,1,3);//[1,3)
        System.out.println(Arrays.toString(array));
        System.out.println("=====================");
        System.out.println(Arrays.toString(copy));
    }

    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5};

        int[] copy = new int[array.length];

        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        System.out.println("array[]"+Arrays.toString(array));
        System.out.println("copy[]"+Arrays.toString(copy));

        System.out.println("=========修改后============");
        copy[0] = 88;

        System.out.println("array[]"+Arrays.toString(array));
        System.out.println("copy[]"+Arrays.toString(copy));
    }

    public static String myToString(int[] array) {
        //Java 里面也有assert
        if(array == null) return "null";
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i != array.length-1) {
                ret += ", ";
            }
        }
        ret+="]";
        return ret;
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5};
        String ret = myToString(null);
        System.out.println(ret);
    }



    public static int[] test3() {
        int[] tmp = {1,2,3,4,5};
        return tmp;
    }

    public static void main5(String[] args) {
        int[] ret = test3();
        System.out.println(Arrays.toString(ret));
    }

    public static void test1(int[] tmpArray) {
        tmpArray = new int[]{11,22,33};
    }

    public static void test2(int[] tmpArray2) {
        tmpArray2[0] = 199;
    }

    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        test2(array);
        System.out.println(Arrays.toString(array));
    }

    public static void show(int[] tmp) {
        for (int i = 0; i < tmp.length; i++) {
            System.out.print(tmp[i]+ " ");
        }
        System.out.println();
    }

    public static void func(int x) {
        x = 100;
    }

    public static void main3(String[] args) {
        /*int[] array = {1,2,3,4,5};
        show(array);
*/
        int a = 10;
        func(a);
        System.out.println(a);
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};//代表这个引用不指向任何的对象
        //System.out.println(array[10]);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+ " ");
        }
        System.out.println();
        for (int x : array) {
            System.out.print(x+" ");
        }
        System.out.println();
        //将array数组 以字符串的形式进行输出
        String ret = Arrays.toString(array);
        System.out.println(ret);
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] array2 = new int[10];
        int[] array3 = new int[]{1,2,3,4,5};
    }
}
